
/**
 * 控制器
 */
define(["jquery","core","core.service","core.handler","angular.util","angular"],function($,core,service,handler,util)
{
	var controllers=angular.module("controllers",[]);
	
	//导航栏控制器
	controllers.controller("NavBarController",["$scope",function($scope)
	{
		var refresh=function()
		{
			service.user.getMessageByToken(function(message)
			{
				service.user.bar.find(function(data)
				{
					$scope.userBars=data;
					$scope.$apply();
				});
				
				if(!core.util.isBlank(message))
					mailPrompt();
			});
		};
		
		var mailPrompt=function()
		{
			service.user.mail.getUnread(function(prompt)
			{
				var content="";
				for(var i=0;i<prompt.length;i++)
				{
					var tempPrompt=prompt[i];
					if(tempPrompt.count!=0)
					{
						var tempContent="";
						tempContent+="<div style='width:100px;' align='center'><a href='#/user/mail?type="+tempPrompt.type+"'>"+tempPrompt.count+"个"+tempPrompt.message+"</a></div>";
						content+=tempContent;
					}
				}
				$scope.mailPrompt=content;
				$scope.$apply();
			});
		};
		
		core.util.onLocationChange(function()
		{
			refresh();
		},false);
		
		refresh();
		
		//登出
		$scope.logout=function()
		{
			service.user.logout(function(result)
			{
				core.ui.simpleDialog(result.message,function()
				{
					if(result.success)
						util.changeLocation(core.System.PATH);
				});
			});
		};
		
		//信息条处理
		$scope.userBarHandle=function(userBar)
		{
			if(!core.util.isBlank(userBar.url))
			{
				util.changeLocation(userBar.url);
				return;
			}
			
			if(!core.util.isBlank(userBar.click))
			{
				$scope[userBar.click]();
				return;
			}
		};
	}]);
	
	//首页控制器
	controllers.controller("IndexController",["$scope",function($scope)
	{
		util.changeTitle("首页");
	}]);
	
	//登录控制器
	controllers.controller("LoginController",["$scope",function($scope)
	{
		util.changeTitle("登录");
		//登录
		$scope.login=function()
		{
			var user=$scope.user;
			service.user.login(user.account,user.password,function(result)
			{
				core.ui.simpleDialog(result.message,function()
				{
					if(result.success)
						util.changeLocation(core.System.PATH);
				});
			});
		};
	}]);
	
	//注册控制器
	controllers.controller("RegisterController",["$scope",function($scope)
	{
		util.changeTitle("注册");
		//注册
		$scope.register=function()
		{
			var user=$scope.user;
			service.user.register(user.account,user.password,function(result)
			{
				core.ui.simpleDialog(result.message,function()
				{
					if(result.success)
					{
						service.user.login(account,password,function()
						{
							util.changeLocation(core.System.PATH);
						});
					}
				});
			});
		};
	}]);
	
	//动弹首页
	controllers.controller("DongtanIndexController",["$scope",function($scope)
	{
		var upToDate=function()
		{
			service.dongtan.upToDate(function(data)
			{
				$scope.newDongtans=data;
				$scope.$apply();
			});
		};

		$scope.upToDate=upToDate;
		
		service.dongtan.hot(function(data)
		{
			$scope.hotDongtans=data;
			$scope.$apply();
		});
		
		//发表动弹
		$scope.create=function()
		{
			var dongtan=$scope.dongtan;
			service.dongtan.create(dongtan,function(result)
			{
				if(result.success)
				{
					core.ui.simpleDialog(core.message.message.DONGTAN_CREATE_SUCCESS);
					dongtan.content="";
					$scope.$apply();
				}
				else
					core.ui.simpleDialog(result.message);
			});
		};
	}]);
	
	//显示所有动弹
	controllers.controller("DongtanShowController",["$scope",function($scope)
	{
		util.changeTitle("大家都在动弹什么?");
		var pn=0;
		var upToDate=function()
		{
			if(pn==null)
				return;
			
			pn++;
			service.dongtan.upToDate(function(data)
			{
				if(core.util.isBlank(data) || core.util.isBlank(data.data))
				{
					core.ui.simpleDialog(core.message.message.DATA_NOT_FOUND);
					pn=null;
					return;
				}
				
				if(!core.util.isBlank($scope.newDongtans))
				{
					var temp=$scope.newDongtans;
					for(var i=0;i<data.data.length;i++)
						temp.data.push(data.data[i]);
					data=temp;
				}
				
				$scope.newDongtans=data;
				$scope.$apply();
			},pn);
		}
		
		service.dongtan.hot(function(data)
		{
			$scope.hotDongtans=data;
			$scope.$apply();
		},1);
		
		upToDate();
		$scope.upToDate=upToDate;
	}]);
	
	//显示动弹
	controllers.controller("DongtanShowOneController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		if(!core.util.isInteger(id) || id<=0)
		{
			util.dataError();
			return;
		}
		service.dongtan.findById(id,function(data)
		{
			if(data==null)
			{
				util.dataError();
				return;
			}
			
			util.changeTitle(data.dongtan.author.username+"的动弹");
			$scope.dongtan=data;
			$scope.$apply();
		});
		
		$scope._comment=
		{
			create:function(comment,callback)
			{
				comment.id=id;
				service.dongtan.comment.create(comment,function(result)
				{
					callback(result);
				});
			}
		};
		$scope.handle=handler.dongtan;
	}]);
	
	//评论控制器
	controllers.controller("CommentIndexController",["$scope","$rootScope",function($scope,$rootScope)
	{
		$scope.$on(core.broadcasts.COMMENT_BEFORE,function(event,option)
		{
			$rootScope.$broadcast(core.broadcasts.COMMENT_AFTER);
			
			var _comment=(function()
			{
				var comments;
				
				return {
					_create:function(comment,callback)
					{
						var newComment={content:comment.content,quote:comment.quote};
						eval("$scope."+option.callback).create(newComment,function(result)
						{
							if(result.success)
								core.ui.simpleDialog(core.message.message.COMMENT_CREATE_SUCCESS);
							else
								core.ui.simpleDialog(result.message);
								
							if(!core.util.isBlank(callback))
								callback(result);
						});
					},
					create:function()
					{
						var comment=$scope.comment;
						comment.quote=0;
						_comment._create(comment,function(result)
						{
							if(result.success)
							{
								comments.data.push(result.message);
								comment.content="";
								$scope.$apply();
							}
						});
					},
					find:function(pn)
					{
						service.comment.findByCommentId(option.id,pn,function(data)
						{
							comments=data;
							$scope.comments=comments;
							$scope.$apply();
						});	
					}
				}
			}());
			
			var _quote=(function()
			{
				return {
					create:function(comment)
					{
						util.angular.dialog.baseDialog
						({
							title:"添加引用",
							path:util.getIncludePath("/comment_quote_create.html"),
							controllerName:"CommentQuoteCreateController"
						},function($createScope,dialog)
						{
							$createScope.save=function()
							{
								var quote=$createScope.quote;
								_comment._create
								({
									content:quote.content,
									quote:comment.id
								},function(result)
								{
									if(result.success)
										dialog.close();
								});
							};
						});
					},
					show:function(comment)
					{
						service.comment.findByQuote(comment.id,1,function(quoteData)
						{
							comment.more=true;
							comment.quoteData=quoteData;
							comment.quotePage=1;
							
							if(quoteData.data.length<quoteData.page_count)
								comment.more=false;
							$scope.$apply();
						});
					},
					close:function(comment)
					{
						comment.more=false;
						comment.quoteData=null;
						comment.quotePage=null;
					},
					more:function(comment)
					{
						var pn=comment.quotePage+1;
						service.comment.findByQuote(comment.id,pn,function(quoteData)
						{
							for(var i=0;i<quoteData.data.length;i++)
								comment.quoteData.data.push(quoteData.data[i]);
							
							if(quoteData.data.length<quoteData.page_count)
								comment.more=false;
							
							comment.quotePage=quoteData.pn;
							$scope.$apply();
						});
					}
				}
			}());
			
			$scope.handle=handler.comment;
			$scope.comment=_comment;
			$scope.quote=_quote;
			_comment.find(1);
		});
	}]);
	
	//openApi
	controllers.controller("OpenApiController",["$scope",function($scope)
	{
		util.changeTitle("Open Api");
		var find=function()
		{
			service.openApi.find(function(data)
			{
				$scope.apis=data;
				$scope.$apply();
			});
		};
		
		find();
		$scope.showOne=function(api)
		{
			$scope.select=api;
		};
	}]);
	
	//发布软件
	controllers.controller("SoftwareCreateController",["$scope",function($scope)
	{
		$scope.save=function()
		{
			service.software.create($scope.software,function(result)
			{
				core.ui.simpleDialog(result.message,function()
				{
					if(result.success)
						util.changeLocation();
				});
			});
		};
	}]);
	
	//显示软件
	controllers.controller("SoftwareShowOneController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		
		if(!core.util.isInteger(id) || id<=0)
		{
			util.dataError();
			return;
		}
		
		service.software.findById(id,function(software)
		{
			if(core.util.isBlank(software))
			{
				util.dataError();
				return;
			}
			
			util.changeTitle(software.software.name+"首页、文档和下载 - "+software.software.title);
			$scope.software=software;
			$scope.$apply();
			
			service.discuss.findBySoftware(id,function(discuss)
			{
				$scope.discuss=discuss;
				$scope.$apply();
			});
		});
		
		$scope.favorite=
		{
			create:function(access)
			{
				handler.favorite.create(service.software.favorite.create,access,access.software,$scope);
			},
			remove:function(access)
			{
				handler.favorite.remove(service.software.favorite.remove,access,access.software,$scope);
			}
		};
		
		$scope.handle=handler.software;
	}]);
	
	//最新推荐软件控制器
	controllers.controller("SoftwareRecommendController",["$scope",function($scope)
	{
		util.changeTitle("最新推荐软件");
		var find=function(pn)
		{
			service.software.findByRecommend(pn,function(software)
			{
				$scope.newSoftware=software;
				$scope.$apply();
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//首页最新推荐软件控制器
	controllers.controller("SoftwareRecommendIndexController",["$scope",function($scope)
	{
		service.software.findByRecommendIndex(function(data)
		{
			$scope.softwares=data;
			$scope.$apply();
		});
	}]);
	
	//发表讨论
	controllers.controller("DiscussCreateController",["$scope","$routeParams",function($scope,$routeParams)
	{
		util.changeTitle("发表讨论");
		service.discuss.forum.find(function(data)
		{
			$scope.forums=data;
			if(core.util.isBlank(data))
			{
				util.dataError();
				return;
			}
			
			var defaultForum=$routeParams.forum;
			if(core.util.isBlank(defaultForum) || defaultForum<=0)
				defaultForum=data[0].id;
			
			for(var i=0,len=data.length;i<len;i++)
			{
				if(data[i].id==defaultForum)
					break;
				
				if(i==len-1)
					defaultForum=data[0].id;
			}
			
			var discuss={forum:defaultForum};
			var tag=$routeParams.tag;
			if(!core.util.isBlank(tag))
			{
				if(!core.util.isArray(tag))
					discuss.tag_1=tag;
				else
				{
					for(var i=0;i<tag.length;i++)
						discuss["tag_"+(i+1)]=tag[i];
				}
			}
			
			$scope.discuss=discuss;
			$scope.$apply();
		});
		
		$scope.save=function()
		{
			var discuss=$scope.discuss;
			service.discuss.create(discuss,function(result)
			{
				core.ui.simpleDialog(result.message,function()
				{
					if(result.success)
						util.changeLocation("/discuss/forum/"+discuss.forum);
				});
			});
		};
	}]);
	
	//讨论首页
	controllers.controller("DiscussIndexController",["$scope",function($scope)
	{
		service.discuss.index(function(data)
		{
			$scope.discuss=data;
			$scope.$apply();
		});
	}]);
	
	//讨论
	controllers.controller("DiscussShowOneController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		if(!core.util.isInteger(id) || id<=0)
		{
			util.dataError();
			return;
		}
		
		service.discuss.findById(id,function(discuss)
		{
			if(core.util.isBlank(discuss))
			{
				util.dataError();
				return;
			}
			
			util.changeTitle(discuss.discuss.title);
			$scope.discuss=discuss;
			$scope.$apply();
		});
		
		$scope._comment=
		{
			create:function(comment,callback)
			{
				comment.id=id;
				service.discuss.comment.create(comment,function(result)
				{
					callback(result);
				});
			}
		};
		
		$scope.favorite=
		{
			create:function(access)
			{
				handler.favorite.create(service.discuss.favorite.create,access,access.discuss,$scope);
			},
			remove:function(access)
			{
				handler.favorite.remove(service.discuss.favorite.remove,access,access.discuss,$scope);
			}
		};
		
		$scope.handle=handler.discuss;
	}]);
	
	//讨论板块首页
	controllers.controller("DiscussForumIndexController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		service.discuss.forum.find(function(forums)
		{
			var forum=null;
			for(var i=0,len=forums.length;i<len;i++)
			{
				if(forums[i].id==id)
				{
					forum=forums[i];
					break;
				}
					
				if(i==len-1)
				{
					id=forums[0].id;
					forum=forums[0];
				}
			}
			
			util.changeTitle(forum.name);
			$scope.forums=forums;
			$scope.forum=forum;
			find(1);
			findTag();
		});
		
		var findTag=function()
		{
			service.discuss.forum.findTag(id,function(tags)
			{
				$scope.tags=tags;
				$scope.$apply();
			});
		};
		
		var find=function(pn)
		{
			service.discuss.findByForum(id,pn,function(data)
			{
				$scope.discuss=data;
				$scope.$apply();
			});
		};
		$scope.find=find;
	}]);
	
	//友情链接
	controllers.controller("FriendlinkIndexController",["$scope",function($scope)
	{
		service.friendlink.find(function(data)
		{
			$scope.friendlinks=data;
			$scope.$apply();
		});
	}]);
	
	//用户资料卡控制器
	controllers.controller("UserMessageCardController",["$scope","$rootScope",function($scope,$rootScope)
	{
		$scope.$on(core.broadcasts.USER_MESSAGE_CARD_BEFORE,function(event,option)
		{
			$rootScope.$broadcast(core.broadcasts.USER_MESSAGE_CARD_AFTER);
			
			var viewHandler=function(data)
			{
				$scope.user=data;
				$scope.option=option;
				$scope.$apply();
			};
			
			service.user.getMessageById(option.uid,function(data)
			{
				viewHandler(data);
			});
			
			$scope.follow=handler.user.follow;
			$scope.mail=handler.user.mail;
		});
	}]);
	
	//用户首页控制器
	controllers.controller("UserHomeController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		if(!core.util.isInteger(id) || id<=0)
		{
			util.dataError();
			return;
		}
		
		var findActive=function(pn)
		{
			service.user.active.findByUser(id,pn,function(data)
			{
				$scope.active=data;
				$scope.$apply();
			});
		};
		
		service.user.home(id,function(home)
		{
			if(core.util.isBlank(home))
			{
				util.dataError();
				return;
			}
			
			service.blog.findNewByAuthor(id,function(newBlogs)
			{
				util.changeTitle(home.user.user.username+"的首页");
				$scope.newBlogs=newBlogs;
				$scope.$apply();
			});
			
			findActive(1);
			
			$scope.home=home;
		});
		
		$scope.follow=handler.user.follow;
		$scope.mail=handler.user.mail;
		$scope.findActive=findActive;
	}]);
	
	//发表博客控制器
	controllers.controller("BlogCreateController",["$scope",function($scope)
	{
		service.blog.catalog.find(function(catalogs)
		{
			if(core.util.isBlank(catalogs) || catalogs.length==0)
			{
				core.ui.simpleDialog("请先添加目录");
				return;
			}
			
			service.blog.order.find(function(orders)
			{
				$scope.orders=orders;
				$scope.catalogs=catalogs;
				$scope.blog={order:orders[0].id,catalog:catalogs[0].id,oneself:false,comment:true};
				$scope.$apply();
			});
			
			$scope.save=function()
			{
				service.blog.create($scope.blog,function(result)
				{
					if(result.success)
						result.message=core.message.message.BLOG_CREATE_SUCCESS;
					
					core.ui.simpleDialog(result.message,function()
					{
						if(result.success)
							util.changeLocation();
					});
				});
			};
		});
	}]);
	
	//博客目录控制器
	controllers.controller("BlogCatalogShowController",["$scope",function($scope)
	{
		service.blog.catalog.find(function(data)
		{
			$scope.catalogs=data;
			$scope.$apply();
		});
		
		$scope.create=function()
		{
			util.angular.dialog.baseCreate
			({
				title:"添加目录",
				path:util.getIncludePath("/blog_catalog_create.html"),
				controllerName:"BlogCatalogCreateController",
				controller:
				{
					func:service.blog.catalog.create,
					$scope:$scope,
					item:"catalog",
					items:$scope.catalogs
				}
			});
		};
		
		$scope.edit=function(catalog)
		{
			util.angular.dialog.baseEdit
			({
				title:"编辑目录",
				path:util.getIncludePath("/blog_catalog_edit.html"),
				controllerName:"BlogCatalogEditController",
				controller:
				{
					func:service.blog.catalog.update,
					$scope:$scope,
					source:catalog,
					item:"catalog",
					items:$scope.catalogs
				}
			});
		};
		
		$scope.remove=function(catalog)
		{
			util.angular.dialog.baseDialog
			({
				title:"目录博文转移",
				path:util.getIncludePath("/blog_catalog_article_move.html"),
				controllerName:"BlogCatalogArticleMoveController"
			},function($removeScope,dialog)
			{
				
				service.blog.catalog.find(function(catalogs)
				{
					util.baseRemove(catalogs,catalog);
					$removeScope.catalogs=catalogs;
					$removeScope.$apply();
				});
				
				$removeScope.remove=function()
				{
					service.blog.catalog.remove(catalog.id,$removeScope.target,function(result)
					{
						core.ui.simpleDialog(result.message,function()
						{
							if(result.success)
							{
								dialog.close();
								if(util.baseRemove($scope.catalogs,catalog))
									$scope.$apply();
							}
						});
					});
				};
				
				$removeScope.cancel=function()
				{
					dialog.close();
				};
			});
		};
	}]);
	
	//最新推荐博客首页控制器
	controllers.controller("BlogRecommendIndexController",["$scope",function($scope)
	{
		service.blog.findRecommend(1,function(data)
		{
			$scope.blog=data;
			$scope.$apply();
		});
	}]);
	
	//显示博客
	controllers.controller("BlogShowOneController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var id=$routeParams.id;
		if(!core.util.isInteger(id) || id<=0)
		{
			util.dataError();
			return;
		}
		
		service.blog.findById(id,function(data)
		{
			if(core.util.isBlank(data))
			{
				util.dataError();
				return;
			}
			
			util.changeTitle(data.blog.title);
			$scope.blog=data;
			$scope.$apply();
		});
		
		$scope._comment=
		{
			create:function(comment,callback)
			{
				comment.id=id;
				service.blog.comment.create(comment,function(result)
				{
					callback(result);
				});
			}
		};
		
		$scope.favorite=
		{
			create:function(access)
			{
				handler.favorite.create(service.blog.favorite.create,access,access.blog,$scope);
			},
			remove:function(access)
			{
				handler.favorite.remove(service.blog.favorite.remove,access,access.blog,$scope);
			}
		};
		
		$scope.follow=
		{
			create:handler.user.follow.create,
			clear:handler.user.follow.clear
		};
		
		$scope.handle=handler.blog;
	}]);
	
	//显示用户博客首页
	controllers.controller("BlogShowController",["$scope","$routeParams",function($scope,$routeParams)
	{
		util.changeTitle("博客");
		var uid=$routeParams.id;
		var catalog=$routeParams.catalog;
		if(!core.util.isInteger(uid) || uid<=0)
		{
			util.dataError();
			return;
		}
		
		var find=function(pn)
		{
			var refreshScope=function(data)
			{
				$scope.blog=data;
				$scope.$apply();
			};
			
			if(core.util.isBlank(catalog) || catalog<=0)
			{
				service.blog.findByAuthor(uid,pn,function(data)
				{
					refreshScope(data);
				});
			}
			else
			{
				service.blog.findByAuthorAndCatalog(uid,catalog,pn,function(data)
				{
					refreshScope(data);
				});
			}
		};
		
		find(1);
		$scope.uid=uid;
		$scope.find=find;
	}]);
	
	//目录引用控制器
	controllers.controller("BlogCatalogIncludeController",["$scope","$rootScope",function($scope,$rootScope)
	{
		$scope.$on(core.broadcasts.BLOG_CATALOG_INCLUDE_BEFORE,function(event,option)
		{
			$rootScope.$broadcast(core.broadcasts.BLOG_CATALOG_INCLUDE_AFTER);
			
			var uid=option.uid;
			$scope.uid=uid;
			
			service.blog.catalog.findByUser(uid,function(catalogs)
			{
				$scope.catalogs=catalogs;
				$scope.$apply();
			});
		});
	}]);
	
	//用户首页管理
	controllers.controller("UserHomeManageController",["$scope",function($scope)
	{
		util.changeTitle("个人管理");
		
		var showOne=function(menu)
		{
			menu.path=util.getIncludePath(menu.url);
			$scope.select=menu;
		};
		
		var find=function()
		{
			service.user.menu.find(function(data)
			{
				if(core.util.isBlank(data))
				{
					util.dataError();
					return;
				}
				
				for(var i=0;i<data.length;i++)
				{
					for(var j=0;j<data[i].children.length;j++)
					{
						if(data[i].children[j].main)
						{
							showOne(data[i].children[j]);
							break;
						}
					}
				}
				$scope.menus=data;
				$scope.$apply();
			});
		};
		
		find();
		$scope.showOne=showOne;
	}]);
	
	//修改个人资料
	controllers.controller("UserMessageEditController",["$scope",function($scope)
	{
		service.user.getMessageByToken(function(user)
		{
			$scope.user=user;
			$scope.$apply();
		},true);
		
		$scope.changeHeadphoto=function(path)
		{
			$scope.user.headphoto=path;
			$scope.$apply();
		};
		
		$scope.save=function()
		{
			service.user.edit($scope.user,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
	}]);
	
	//显示邮件控制器
	controllers.controller("UserMailShowController",["$scope","$routeParams",function($scope,$routeParams)
	{
		util.changeTitle("我的邮件");
		
		var type=$routeParams.type;
		service.user.mail.type.find(function(data)
		{
			if(core.util.isBlank(type))
			{
				type=data[0].id;
				data[0].select=true;
			}
			else
			{
				for(var i=0;i<data.length;i++)
				{
					if(data[i].id==type)
					{
						data[i].select=true;
						break;
					}
					
					if(i==data.length-1)
						type=data[0].id;
				}
			}
			find(1);
			$scope.types=data;
			$scope.$apply();
		});
		
		var find=function(pn)
		{
			service.user.mail.findByType(type,pn,function(data)
			{
				$scope.mail=data;
				$scope.$apply();
			});
		};
		
		$scope.pre=function(mail)
		{
			util.angular.dialog.baseDialog
			({
				title:"查看",
				path:util.getIncludePath("/user_mail_pre.html"),
				controllerName:"UserMailPreController"
			},function($preScope)
			{
				$preScope.mail=mail;
				$preScope.remove=$scope.remove;
				$preScope.setRead=$scope.setRead;
			});
		};
		
		$scope.create=function()
		{
			handler.user.mail.create();
		};
		
		$scope.remove=function(mail)
		{
			handler.user.mail.remove(mail,function(result)
			{
				if(result.success)
				{
					mail.remove=true;
					$scope.$apply();
				}
			});
		};
		
		$scope.setRead=function(mail)
		{
			handler.user.mail.setRead(mail,function()
			{
				mail.read=true;
				$scope.$apply();
			});
		};
		$scope.find=find;
	}]);
	
	//搜索首页
	controllers.controller("SearchIndexController",["$scope",function($scope)
	{
		util.changeTitle("搜索");
	}]);
	
	//搜索软件
	controllers.controller("SearchSoftwareController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var keyword=$routeParams.keyword;
		$scope.keyword=keyword;
		var find=function(pn)
		{
			if(core.util.isBlank(keyword))
				return;
			
			service.software.search(keyword,function(data)
			{
				$scope.result=data;
				$scope.$apply();
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//搜索博客
	controllers.controller("SearchBlogController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var keyword=$routeParams.keyword;
		$scope.keyword=keyword;
		var find=function(pn)
		{
			if(core.util.isBlank(keyword))
				return;
			
			service.blog.search(keyword,function(data)
			{
				$scope.result=data;
				$scope.$apply();
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//搜索讨论
	controllers.controller("SearchDiscussController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var keyword=$routeParams.keyword;
		$scope.keyword=keyword;
		var find=function(pn)
		{
			if(core.util.isBlank(keyword))
				return;
			
			service.discuss.search(keyword,function(data)
			{
				$scope.result=data;
				$scope.$apply();
			});
		};
		
		find(1);
		$scope.find=find;
	}]);
	
	//收藏控制器
	controllers.controller("FavoriteShowController",["$scope",function($scope)
	{
		var find=function(pn)
		{
			service.favorite.findByType($scope.select.id,pn,function(favorite)
			{
				$scope.favorite=favorite;
				$scope.$apply();
			});
		};
		
		var selectType=function(type)
		{
			$scope.select=type;
			find(1);
		};
		
		service.favorite.type.find(function(types)
		{
			$scope.types=types;
			$scope.$apply();
			selectType(types[0]);
		});
		
		$scope.find=find;
		$scope.selectType=selectType;
	}]);
	
	//修改密码控制器
	controllers.controller("UserPasswordUpdateController",["$scope",function($scope)
	{
		$scope.user={oldPassword:"",newPassword:"",checkNewPassword:""};
		
		$scope.save=function()
		{
			service.user.updatePassword($scope.user,function(result)
			{
				core.ui.simpleDialog(result.message);
			});
		};
	}]);
	
	//注册认证控制器
	controllers.controller("UserRegisterCheckController",["$scope","$routeParams",function($scope,$routeParams)
	{
		var identity=$routeParams.identity;
		if(core.util.isBlank(identity))
		{
			util.dataError();
			return;
		}
		
		service.user.registerCheck(identity,function(result)
		{
			core.ui.simpleDialog(result.message,function()
			{
				util.changeLocation();
			});
		});
	}]);
});